package org.jeecg.modules.cost.service.impl;

import org.jeecg.modules.cost.entity.MtCostChange;
import org.jeecg.modules.cost.entity.MtCostChangeItem;
import org.jeecg.modules.cost.mapper.MtCostChangeItemMapper;
import org.jeecg.modules.cost.mapper.MtCostChangeMapper;
import org.jeecg.modules.cost.service.IMtCostChangeService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 成本调整
 * @Author: jeecg-boot
 * @Date:   2023-12-18
 * @Version: V1.0
 */
@Service
public class MtCostChangeServiceImpl extends ServiceImpl<MtCostChangeMapper, MtCostChange> implements IMtCostChangeService {

	@Autowired
	private MtCostChangeMapper mtCostChangeMapper;
	@Autowired
	private MtCostChangeItemMapper mtCostChangeItemMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(MtCostChange mtCostChange, List<MtCostChangeItem> mtCostChangeItemList) {
		mtCostChangeMapper.insert(mtCostChange);
		if(mtCostChangeItemList!=null && mtCostChangeItemList.size()>0) {
			for(MtCostChangeItem entity:mtCostChangeItemList) {
				//外键设置
				entity.setCostId(mtCostChange.getId());
				mtCostChangeItemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(MtCostChange mtCostChange,List<MtCostChangeItem> mtCostChangeItemList) {
		mtCostChangeMapper.updateById(mtCostChange);

		//1.先删除子表数据
		mtCostChangeItemMapper.deleteByMainId(mtCostChange.getId());

		//2.子表数据重新插入
		if(mtCostChangeItemList!=null && mtCostChangeItemList.size()>0) {
			for(MtCostChangeItem entity:mtCostChangeItemList) {
				//外键设置
				entity.setCostId(mtCostChange.getId());
				mtCostChangeItemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		mtCostChangeItemMapper.deleteByMainId(id);
		mtCostChangeMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			mtCostChangeItemMapper.deleteByMainId(id.toString());
			mtCostChangeMapper.deleteById(id);
		}
	}

}
